﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using Besture.Bll;
using Besture.Common;
using Besture.Common.Definition;
using Besture.Model;
using Besture.Utils;

namespace Besture.UI.Admin.Product
{
    public partial class ImageSet : BasePage
    {
        #region 页面加载事件

        /// <summary>
        /// 页面加载
        /// </summary>
        protected override void InitialLoad()
        {
           if(!IsPostBack)
           {
               //绑定图片类别
               DictionaryDac dac = new DictionaryDac();
               Utils.ControlUtil.BindDropDownList(ddlImageType, dac.GetDataTableByType(Constants.DICTIONARY_TYPE_ProductClass), "Name", "Code");
            if(Request.QueryString["id"] != null && !string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                //编辑编号
                hidEditID.Value = Request.QueryString["id"];                   
                if(!Fun.IsNumber(hidEditID.Value))
                {
                    RedirectErrorUrl("编号错误");
                }               
                InitData(Convert.ToInt32(hidEditID.Value));               
            }           
           }
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {

        }
        
        /// <summary>
        /// 数据加载
        /// </summary>
        /// <param name="id">编号</param>
        protected void InitData(int id)
        {
            ImageDac dac = new ImageDac();
            ImageModel model = dac.GetModel(id);
            txtrContentDetail.Text = model.ImageRemark;
            txtImageUrl.Text = model.ImageLink;
			ddlImageType.SelectedValue = model.ImageType.ToString();
            UploadSinglePhoto1.ImageUrl = model.ImageUrl != null ? model.ImageUrl.ToString() : "";
            //txtSortID.Text = model.SortID!=null?model.SortID.ToString():"";
        }
        #endregion

        #region 保存

        protected void btnSubmit_Click(object sender, EventArgs eventArgs)
        {
            ImageDac dac = new ImageDac();
            ImageModel model = null;
            string editID = hidEditID.Value;
            //添加
            if(string.IsNullOrEmpty(editID))
            {
                model = new ImageModel();
                model.ImageRemark = txtrContentDetail.Text.Trim();
                model.ImageUrl = UploadSinglePhoto1.ImageUrl;
				model.ImageType = ddlImageType.SelectedValue!=""?Convert.ToInt32(ddlImageType.SelectedValue):0;
                model.ImageLink =txtImageUrl.Text.Trim();
                model.IsDeleted = 0;
                model.CreateBy = AdminUserService.AdminLoginUser.ID;
                model.CreateTime = DateTime.Now;
                model.UpdateTime = DateTime.Now;

				//model.SortID =txtSortID.Text.Trim()!=""?Convert.ToInt32(txtSortID.Text.Trim()):0;
				
                dac.Add(model);
            }
            //编辑
            else
            {
                model = dac.GetModel(Convert.ToInt32(editID));
                model.ImageRemark = txtrContentDetail.Text.Trim();
                model.ImageUrl = UploadSinglePhoto1.ImageUrl;
				model.ImageType = ddlImageType.SelectedValue!=""?Convert.ToInt32( ddlImageType.SelectedValue):0;
                model.ImageLink = txtImageUrl.Text.Trim();
                model.IsDeleted = 0;
                model.UpdateBy = AdminUserService.AdminLoginUser.ID;
                model.UpdateTime = DateTime.Now;
				//model.SortID = txtSortID.Text.Trim()!=""?Convert.ToInt32( txtSortID.Text.Trim()):0;
				
                dac.Update(model);
            }
            Fun.Alert("保存成功", "ImageList.aspx");
        }
        #endregion

        #region 取消

        public void btnCanel_Click(object sender, EventArgs eventArgs)
        {
            RedirectUrl("ImageList.aspx");
        }

        #endregion

       
    }
}
